package myservlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/* this is the Login Servlet */
public class LoginServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String s = "/home/ec2-user/AwsCredentials.properties";

	public void doPost(HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException{
		String username;
		String password;
		res.setContentType("text/html");
		// PrintWriter out = res.getWriter();

		FileInputStream fis = new FileInputStream("/home/ec2-user/AwsCredentials.properties");
		OutputStream os = res.getOutputStream();
		int r = -1;

		username = req.getParameter("username");
		password = req.getParameter("password");
		Credential c = new Credential(username, password);

		if (c.IsCorrect() == true) {
			// out.println("1Dude, you are right!1"); 
			while((r=fis.read())!=-1){
				os.write(r);
			}
			os.flush();
		} else {
			// out.println("0Dude, you are wrong!1");
//			os.write(-1);
			os.flush();
		}
		os.close();
	}
}
